package com.zb.controller;

import javax.annotation.Resource;

import com.zb.entity.Item;
import com.zb.exception.MyException;
import com.zb.service.ItemService;
import com.zb.util.SortUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.zb.entity.Collection;
import com.zb.service.CollectionService;
import com.zb.util.Base64Util;
import com.zb.util.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * 收藏controller
 *
 * @author HBY
 */
@Controller
@RequestMapping("/api/collection")

public class CollectionController {

    @Resource
    private CollectionService collectionService;
    @Resource
    private ItemService itemService;

    /**
     * 收藏中添加商品
     *
     * @param userId
     * @param itemId
     * @return Result
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result addItem(@RequestParam("userId") String userId , @RequestParam("itemId") Long itemId){

        collectionService.addItem(Base64Util.decode(userId) , itemId);
        return Result.ok("收藏成功" , null);
    }

    /**
     * 收藏中移除商品
     *
     * @param collectionId
     * @param itemId
     * @return Result
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Result removeItem(@RequestParam("collectionId") String collectionId , @RequestParam("itemId") Long itemId ){
        collectionService.removeItem(Base64Util.decode(collectionId) , itemId);
        return Result.ok("收藏已移除" , null);
    }

    /**
     * 详情界面取消收藏商品
     *
     * @param userId
     * @param itemId
     * @return Result
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result deleteItem(@RequestParam("userId") String userId ,@RequestParam("itemId") Long itemId){
        Collection collection = collectionService.findByUser(Base64Util.decode(userId));
        collectionService.removeItem(collection.getId() , itemId);
        return Result.ok("已取消收藏" , null);

    }


    /**
     * 判断是否已收藏商品
     *
     * @param userId
     * @param itemId
     * @return Result
     */
    @RequestMapping("/ifCollected")
    @ResponseBody
    public Result ifCollected(@RequestParam("userId") String userId ,@RequestParam("itemId") Long itemId){
        Collection collection = collectionService.findByUser(Base64Util.decode(userId));
        if(collectionService.hasItem(itemId,collection.getId())){
            System.out.println("已收藏：userId"+userId+"itemId"+itemId);
            return Result.ok("true" , null);
        }else{
            System.out.println("未收藏：userId"+userId+"itemId"+itemId);
            return Result.ok("false" , null);
        }
    }

    /**
     * 获取商品的收藏量排行
     *
     * @return
     */
    @GetMapping("/itemCollectionRanking")
    @ResponseBody
    public List<Map.Entry<String, Long>> getItemCollectionRanking() {
        List<Item> items = itemService.getAllItems();
        // map 存储<id,粉丝数>
        Map<String, Long> collectionCountMap = new HashMap<>(16);
        for (Item item : items) {
            String name = item.getItemName();
            long collectionCnt = collectionService.getCollectionCount(item.getId());
            collectionCountMap.put(name, collectionCnt);
        }
        List<Map.Entry<String, Long>> ranking = SortUtil.mapSort(collectionCountMap);

        return ranking;
    }
}
